package com.arcgis.app.controller.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.arcgis.app.MainActivity;
import com.arcgis.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PointListFragment extends Fragment {
    //    @BindView(R.id.fragment_search)
    SearchView fragmentSearch;
    @BindView(R.id.list_lv_all)
    LinearLayout listLvAll;
    @BindView(R.id.list_lv_point)
    LinearLayout listLvPoint;
    @BindView(R.id.list_lv_line)
    LinearLayout listLvLine;
    @BindView(R.id.list_lv_area)
    LinearLayout listLvArea;

    private LinearLayout lastSelectedLayout;

    public enum ListEnumType {
        ALL,
        POINT,
        LINE,
        AREA,
    }

    public ListEnumType listEnumType = ListEnumType.ALL;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lastSelectedLayout = listLvAll;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.list_layout, container, false);
        ButterKnife.bind(this, view);
        listLvAll.setSelected(true);
        fragmentSearch = (SearchView) view.findViewById(R.id.fragment_search);
        setListener(fragmentSearch);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listLvAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastSelectedLayout != null) {

                    lastSelectedLayout.setSelected(false);
                }
                listLvAll.setSelected(true);
                lastSelectedLayout = listLvAll;
                listEnumType = ListEnumType.ALL;
                changeMode();
            }

        });
        listLvPoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastSelectedLayout != null) {

                    lastSelectedLayout.setSelected(false);
                }
                listLvPoint.setSelected(true);
                lastSelectedLayout = listLvPoint;
                listEnumType = ListEnumType.POINT;
                changeMode();

            }
        });
        listLvLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastSelectedLayout != null) {

                    lastSelectedLayout.setSelected(false);
                }
                listLvLine.setSelected(true);
                lastSelectedLayout = listLvLine;
                listEnumType = ListEnumType.LINE;
                changeMode();

            }
        });
        listLvArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastSelectedLayout != null) {

                    lastSelectedLayout.setSelected(false);
                }
                listLvArea.setSelected(true);
                lastSelectedLayout = listLvArea;
                listEnumType = ListEnumType.AREA;
                changeMode();
            }
        });
    }

    private MainActivity mainActivity() {
        return (MainActivity) getActivity();
    }

    public void setListener(SearchView view) {
        // 设置搜索文本监听
        view.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
//                LogUtil.e(MainActivity.class,"=====query="+query);
                mainActivity().search(query.trim());
                return false;
            }

            //当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
//                LogUtil.e(MainActivity.class,"=====newText="+newText);
                mainActivity().search(newText.trim());
                return false;
            }
        });
    }



    @Override
    public void onPause() {
        super.onPause();
    }

    public  void changeMode(){
        mainActivity().changeListDataMode(listEnumType);
//        switch (listEnumType){
//            case ALL:
//
//                break;
//            case POINT:
//                break;
//            case LINE:
//                break;
//            case AREA:
//                break;
//        }
    }
}
